package com.seehope.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.seehope.po.User;
import com.seehope.service.UserService;
import com.seehope.service.impl.UserServiceImpl;

/**
 * Servlet implementation class AddUserServlet
 */
@WebServlet(name = "AddUserServlet", urlPatterns = "/addUserServlet")
public class AddUserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
    private UserService userService =new UserServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        JSONObject jsonObject=new JSONObject();
		String username =request.getParameter("username");
		String password =request.getParameter("password");
		String fullname =request.getParameter("fullname");
		String phone =request.getParameter("phone");
		User user =new User();
		user.setUsername(username);
		user.setFullname(fullname);
		user.setPassword(password);
		user.setPhone(phone);
		user.setActive(0);
		int count= userService.addUser(user);
		if(count>0){
			 jsonObject.put("code",1);
		     jsonObject.put("msg","添加成功");
		}else{
			 jsonObject.put("code",0);
		     jsonObject.put("msg","添加失败");
		}
		out.println(jsonObject.toString());
        out.flush();
        out.close();
	}

}
